import 'package:dropdown_search/dropdown_search.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:glorysoft_mobile_plugin/tool/extensions/extension.dart';
import 'package:xin_nuo_pda/views/search_drop_down_select/search_drop_down_select_controller.dart';

import '../../values/colors.dart';
import '../widget_utils.dart';

class SearchDropDownSelectWidget extends StatelessWidget {
  final logic = Get.put(SearchDropDownSelectController());
  static const String label = "label";
  static const String value = "value";

  String? hint;
  List<Map<dynamic, dynamic>> mShowList;
  double? height;
  ValueChanged<Map<dynamic, dynamic>?>? onChanged;
  bool? isShowSearch;
  Map<dynamic, dynamic>? selectObj;

  SearchDropDownSelectWidget({
    this.hint = "请选择",
    required this.mShowList,
    required this.onChanged,
    this.isShowSearch = false,
    required this.selectObj,
    this.height = 40,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height,
      child: DropdownSearch<Map<dynamic, dynamic>>(
        // maxHeight:mShowList.length< 4 ? (56 * mShowList.length) :null,
        validator: (v) => v == null ? "required field" : null,
        dropdownSearchTextAlignVertical: TextAlignVertical.center,
        dropdownSearchDecoration: InputDecoration(
          contentPadding: const EdgeInsets.fromLTRB(12, 0, 0, 0),
          fillColor: Colors.grey[200],
          //背景颜色，必须结合filled: true,才有效
          filled: true,
          focusedBorder: WidgetsUtils.outlineInputBorder,
          enabledBorder: WidgetsUtils.outlineInputBorder,
          disabledBorder: WidgetsUtils.outlineInputBorder,
          focusedErrorBorder: WidgetsUtils.outlineInputBorder,
          errorBorder: WidgetsUtils.outlineInputBorder,
        ),
        mode: Mode.MENU,
        showSearchBox: isShowSearch ?? false,
        items: mShowList,
        showSelectedItems: true,
        selectedItem: selectObj,
        compareFn: (i, s) => i?[value] == (s?[value]),
        popupItemBuilder: (context, element, isSelected) {
          return ListTile(
              title: Text(
            element[label],
            style: TextStyle(color: text_black.toColor(), fontSize: 14),
          ));
        },
        dropdownBuilder: (context, element) {
          return TextField(
            enabled: false,
            decoration: InputDecoration(
                hintText: hint,
                labelText: element?[label],
                isCollapsed: true,
                hintStyle: TextStyle(fontSize: 14, color: text_gray.toColor()),
                labelStyle:
                    TextStyle(fontSize: 14, color: text_black.toColor()),
                border: InputBorder.none),
          );
        },
        itemAsString: (element) {
          return element?[label].toString() ?? '';
        },
        onChanged: onChanged,
      ),
    );
  }
}
